import os
import sys
import random

import pygame

# 数字迷宫游戏

from game.snake.entity.block import Block
from game.snake.utils.block_action import BlockAction
from game.snake.utils.utils import Utils

pygame.init()


# 移动步长
step = 50
width_screen = 20
height_screen = 15
# 背景颜色
bg_color = (255,255,255)
# 设置窗口大小和标题
screen = pygame.display.set_mode((step*width_screen, step*height_screen))
pygame.display.set_caption("数字迷宫小游戏")

# 目标分数
score_target = 10;


def game_loop():
    # 文件路径
    main_dir = os.path.split(os.path.abspath(__file__))[0]
    # 方块图片
    block_img_bee_path = "bee.png"
    # 得分
    score = 0
    # 运行状态
    running = True
    # 方块集合
    block_group = []
    block_main = Block(0,0,step,step,(0,0,0))
    block_main.setText(pygame,"0")
    block_main.setImage(Utils.load_image(block_img_bee_path,(step,step),main_dir))
    block_main_group = []
    block_main_group.append(block_main)
    # 方块的上一个位置
    block_last =[1]
    block_last[0] =Block(0,0,step,step,(0,0,0))
    message = "doing"

# 创建字体对象并设置字体和字号
    font = pygame.font.Font(None, 36)
    # 在屏幕右上角显示得分
    score_color = (255, 0, 0)
    score_text = font.render("Score: " + str(score)+", Target: "+str(score_target), True, score_color)
    socore_text_local = (600, 10)
    screen.blit(score_text, socore_text_local)
    for x in range(1,10):
        for y in range(1,10):
            block_y = Block(x*step,y*step,step,step,(255,255,255))
            block_y.border =1
            # block_y.color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
            # if x == 0 or y == 0 or x % 9 ==0 or y % 9 == 0 :
            #     if (x==0 and y==0) or (x % 9 ==0 and y % 9 == 0):
            #         block_y.setText(pygame,"1")
            #     else:
            #         block_y.setText(pygame,"0")
            # else:
            if x ==0 :
                block_y.setText(pygame,"0")
            else:
                block_y.setText(pygame,str(random.randint(0, 3)))
            block_group.append(block_y)


    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT :
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                # 原地掉头
                print("按空格键了")
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print("鼠标单机")

            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                if block_main_group[0].x -step >=0:
                    BlockAction.moveAll(-step,0,block_main_group,"left",block_last)
            if keys[pygame.K_RIGHT]:
                if block_main_group[0].x +step < screen.get_width():
                    BlockAction.moveAll(step,0,block_main_group,"right",block_last)
            if keys[pygame.K_UP]:
                if block_main_group[0].y -step >=0:
                    BlockAction.moveAll(0,-step,block_main_group,"up",block_last)
            if keys[pygame.K_DOWN]:
                if block_main_group[0].y + step < screen.get_height():
                    BlockAction.moveAll(0,step,block_main_group,"down",block_last)

        # 获得碰撞的方块
        block_score = [ x for x in block_group if pygame.Rect(block_main_group[0].show()).colliderect(pygame.Rect(x.show())) ]

        if len(block_score)>0:
            message = "doing"
            if block_score[0].zhan == 0:
                if int(block_score[0].text) == 0 :
                   block_main_group[0] = block_last[0]
                else:
                    score+= int(block_score[0].text)
                    block_main_group[0].setText(pygame,str(score))
                    block_score[0].setText(pygame,"-1")
                    block_score[0].color = (0,0,0)
                    block_score[0].zhan = 1
                    # block_score[0].se
                    print(block_main_group[0].text)
            # else:
            #     block_main_group[0] = block_last[0]
        else:
            if block_last[0].show() == (9*step,9*step,step,step):
                print("sucess")
                message = "sucess"
            else:
                print("出界了")
                message = "error"
        #设置背景颜色
        screen.fill(bg_color)
        #绘制方块
        # block_main.draw(screen,pygame)
        BlockAction.drawAllNoOrder(block_group,screen)
        BlockAction.drawAllNoOrder(block_main_group,screen)
        # 绘制分数
        score_text = font.render("Score: " + str(block_main_group[0].text)+", Target:"+str(score_target)+",Result:"+str(message), True, score_color)
        screen.blit(score_text, socore_text_local)
        # 更新屏幕显示
        pygame.display.update()

    # 退出游戏
    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    game_loop()
